var app = new Vue({
	el: '#app',
	// delimiters: ['[[', ']]'], //修改vue读取变量的语法 {{}}改为[[]]
	data: {
		host: host,
		//v-model
		username: '',
		password: '',
		mobile: '',
		remembered: '',

		error_username: false,
		error_password: false,


		error_username_message: '请输入用户名或者账号登录',
		error_password_message: '密码错误',
	},
	methods: {
		check_username: function() {
			let that = this;
			var re = /^[a-zA-Z0-9_-]{5,20}$/;
			var res = /^1[3-9]\d{9}$/;
			if (re.test(that.username) || res.test(that.username)) {
				that.error_username = false;
			} else {
				that.error_username_message = '请输入5-20位有效用户名';
				that.error_username = true
			}
		},
		check_pwd: function() {
			var re = /^[0-9A-Za-z]{5,20}$/;
			if (re.test(this.password)) {
				this.error_password = false;
			} else {
				this.error_password_message = '请输入5-20位的英文字母、数字、下划线的密码';
				this.error_password = true
			}
		},
		// oa登录
		// oa_login: function() {
		// 	// var next = get_query_string('next') || '/';
		// 	axios.get(this.host + '/qq/authorization/' , {
		// 		responseType: 'json',
		// 		withCredentials: true,
		// 	}).then(function(resp) {
		// 			console.log(resp.data.login_url);
		// 			location.href = resp.data.login_url;
				
		// 	}).catch(function() {
		// 		console.log('xxx');
		// 	})
		// },

		on_submit: function() {

			let that = this;
			that.check_username();
			that.check_pwd();
			if (that.error_username == true || that.error_password == true) {
				//如果以上有一个错误提示则禁用表单提交按钮
				window.event.returnValue = false;
			} else {    
				var url = that.host + '/login/';
				console.log(that.host + '/login/');
				axios.post(url, {
						username: that.username,
						password: that.password,
						remember: that.remember,
					}, {
						responseType: 'json',
						withCredentials: true,
					})
					.then(resp => {
						// alert('nihao')
						console.log(resp.data.code);
						var next = get_query_string('next')
						if (resp.data.code == 200) {
							if (next){
								location.href = next;
							}else{
								location.href = 'index.html';
							}
							
						} else {
							if (resp.data.code == 400) {
								//alert(resp.data.error)
								// alert('sddsadasdsadas')
								that.error_password_message = resp.data.error;
								that.error_password = true;
							}
						}
					})
					.catch(error => {
						alert('内部错误，联系管理员')
					})
			}
		},

	}

})
